package com.roamer.rest;

import org.springframework.http.HttpStatus;

/**
 * API 统一响应代码接口
 *
 * @author roamer
 * @version V1.0
 * @date 2019-07-17 15:43
 */
public interface ResultCode {

    /**
     * API 正确响应代码接口
     */
    interface SuccessCode extends ResultCode {

        /**
         * 成功
         *
         * @return {@code true} 成功
         */
        @Override
        default boolean success() {
            return true;
        }
    }

    /**
     * API 错误响应码接口
     */
    interface ErrorCode extends ResultCode {

        /**
         * 失败
         *
         * @return {@code false} 失败
         */
        @Override
        default boolean success() {
            return false;
        }
    }

    /**
     * 获取响应码
     *
     * @return {@code int} 响应码
     */
    int getCode();

    /**
     * 获取提示信息
     *
     * @return {@code String} 提示消息
     */
    String getMessage();

    /**
     * 状态值
     *
     * @return {@code boolean} 成功标示
     */
    boolean success();

    /**
     * 获取httpStatus
     *
     * @return {@link org.springframework.http.HttpStatus}
     */
    default HttpStatus httpStatus() {
        HttpStatus status = HttpStatus.resolve(getCode() / 100);
        if (null == status) {
            status = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return status;
    }
}
